import math

hanzi = {
	# [狗], size: (21 * 18)
	0xe78b97: [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x90, 0x00, 0x05, 0x20, 0x00, 0x02, 0x3F, 0x10, 0x05, 0x40,
	           0x10, 0x09, 0x80, 0x10, 0x11, 0x3C, 0x10, 0x01, 0x24, 0x10, 0x03, 0x24, 0x10, 0x05, 0x24, 0x10, 0x09,
	           0x3C, 0x10, 0x11, 0x24, 0x10, 0x01, 0x00, 0x10, 0x01, 0x00, 0x10, 0x0A, 0x05, 0x00, 0x04, 0x02, 0x00,
	           0x00, 0x00, 0x00],

	# [,], size: (21 * 18)
	0x2c: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	       0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00],

	# [ ], size: (21 * 18)
	0x20: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],

	# [是], size: (21 * 18)
	0xe698af: [0x00, 0x00, 0x00, 0x03, 0xFE, 0x00, 0x02, 0x02, 0x00, 0x02, 0x02, 0x00, 0x03, 0xFE, 0x00, 0x02, 0x02,
	           0x00, 0x02, 0x02, 0x00, 0x03, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0x18, 0x00, 0x20, 0x00, 0x02,
	           0x20, 0x00, 0x02, 0x3F, 0x00, 0x02, 0x20, 0x00, 0x05, 0x20, 0x00, 0x08, 0xA0, 0x00, 0x10, 0x7F, 0x18,
	           0x00, 0x00, 0x00],

	# [人], size: (21 * 18)
	0xe4baba: [0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x20, 0x00, 0x00, 0x20, 0x00, 0x00, 0x20, 0x00, 0x00, 0x20,
	           0x00, 0x00, 0x20, 0x00, 0x00, 0x50, 0x00, 0x00, 0x50, 0x00, 0x00, 0x88, 0x00, 0x00, 0x88, 0x00, 0x01,
	           0x04, 0x00, 0x01, 0x04, 0x00, 0x02, 0x02, 0x00, 0x04, 0x01, 0x00, 0x08, 0x00, 0x10, 0x10, 0x00, 0x08,
	           0x00, 0x00, 0x00],

	# [类], size: (21 * 18)
	0xe7b1bb: [0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x02, 0x22, 0x00, 0x01, 0x24, 0x00, 0x00, 0x20, 0x00, 0x0F, 0xFF,
	           0x10, 0x00, 0xA8, 0x00, 0x01, 0x24, 0x00, 0x02, 0x22, 0x00, 0x0C, 0x01, 0x00, 0x00, 0x20, 0x00, 0x1F,
	           0xFF, 0x18, 0x00, 0x50, 0x00, 0x00, 0x88, 0x00, 0x01, 0x04, 0x00, 0x06, 0x03, 0x00, 0x18, 0x00, 0x18,
	           0x00, 0x00, 0x00],

	# [最], size: (21 * 18)
	0xe69c80: [0x00, 0x00, 0x00, 0x03, 0xFE, 0x00, 0x02, 0x02, 0x00, 0x03, 0xFE, 0x00, 0x02, 0x02, 0x00, 0x03, 0xFE,
	           0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0x18, 0x04, 0x40, 0x00, 0x07, 0xDF, 0x00, 0x04, 0x51, 0x00, 0x07,
	           0xD2, 0x00, 0x04, 0x4A, 0x00, 0x05, 0xE4, 0x00, 0x1E, 0x4A, 0x00, 0x08, 0x51, 0x00, 0x00, 0x60, 0x18,
	           0x00, 0x00, 0x00],

	# [好], size: (21 * 18)
	0xe5a5bd: [0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x1F, 0x10, 0x02, 0x00, 0x10, 0x02, 0x01, 0x00, 0x1F, 0x82,
	           0x00, 0x04, 0x84, 0x00, 0x04, 0x84, 0x00, 0x04, 0xBF, 0x18, 0x04, 0x84, 0x00, 0x09, 0x04, 0x00, 0x05,
	           0x04, 0x00, 0x02, 0x04, 0x00, 0x05, 0x04, 0x00, 0x08, 0x84, 0x00, 0x10, 0x94, 0x00, 0x00, 0x08, 0x00,
	           0x00, 0x00, 0x00],

	# [的], size: (21 * 18)
	0xe79a84: [0x00, 0x00, 0x00, 0x02, 0x08, 0x00, 0x02, 0x08, 0x00, 0x04, 0x08, 0x00, 0x0F, 0xCF, 0x10, 0x08, 0x50,
	           0x10, 0x08, 0x50, 0x10, 0x08, 0x60, 0x10, 0x08, 0x48, 0x10, 0x0F, 0xC4, 0x10, 0x08, 0x44, 0x10, 0x08,
	           0x40, 0x10, 0x08, 0x40, 0x10, 0x08, 0x40, 0x10, 0x0F, 0xC0, 0x10, 0x08, 0x45, 0x00, 0x00, 0x02, 0x00,
	           0x00, 0x00, 0x00],

	# [朋], size: (21 * 18)
	0xe69c8b: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xCF, 0x10, 0x04, 0x48, 0x10, 0x04, 0x48, 0x10, 0x04, 0x48,
	           0x10, 0x07, 0xCF, 0x10, 0x04, 0x48, 0x10, 0x04, 0x48, 0x10, 0x04, 0x48, 0x10, 0x07, 0xCF, 0x10, 0x04,
	           0x48, 0x10, 0x04, 0x48, 0x10, 0x04, 0x48, 0x10, 0x08, 0x50, 0x10, 0x09, 0x52, 0x10, 0x10, 0xA1, 0x00,
	           0x00, 0x00, 0x00],

	# [友], size: (21 * 18)
	0xe58f8b: [0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x1F, 0xFF, 0x18, 0x00, 0x80,
	           0x00, 0x00, 0x80, 0x00, 0x01, 0xFE, 0x00, 0x01, 0x42, 0x00, 0x02, 0x42, 0x00, 0x02, 0x24, 0x00, 0x04,
	           0x28, 0x00, 0x08, 0x10, 0x00, 0x10, 0x28, 0x00, 0x00, 0xC4, 0x00, 0x03, 0x03, 0x00, 0x0C, 0x00, 0x18,
	           0x00, 0x00, 0x00]
}


def show(words: str, width=21, height=18, ignore=True) -> None:
	"""
	显示汉字字模
	:param words: 要显示的文字（中英文）
	:param width: 字模高度
	:param height: 字模宽度
	:param ignore: 是否忽略错误
	"""

	# 枚举所有汉字
	for word in words:
		word_code = 0x0  # 文字的 Unicode 编码

		code = word.encode("utf-8")

		# 根据字节长度判断中英文
		if len(code) == 3:
			word_code |= code[0] << 16
			word_code |= code[1] << 8
			word_code |= code[2]
		else:
			word_code |= code[0]

		try:
			# 从字体库读取字模，如果不存在则给出错误提示
			matrix_array = hanzi[word_code]
		except KeyError:
			if ignore:
				print("Missing matrix define: '{0}'\n".format(word))

				continue
			else:
				raise KeyError("Missing matrix define: ", word)

		matrix_index = 0  # 字模索引下标

		# 按行显示字模
		for count in range(0, height):
			row_count = 0  # 每行打印点阵数量统计

			# 根据每行宽度计算打印次数，也就是每行需要从字模列表里取几次数据
			for times in range(0, math.ceil(width / 8)):
				# 将取到的字模数据转换为二进制字符串（8 位 0 和 1 的组合）
				# 因为字模宽度不一定为 8 的整倍数，所以在取每行最后一个数据时，
				# 需要根据实际情况获取指定位数的二进制字符串
				binary = bin(matrix_array[matrix_index]).replace('0b', '')\
					.rjust(width % 8 if times + 1 == math.ceil(width / 8) else 8, '0')
				matrix_index += 1

				# 顺序打印每行点阵
				for bit in binary:
					print("●" if bit == '1' else "○", end='')

					row_count += 1

					# 如果已打印的点阵数量达到指定宽度，
					# 则跳出循环并开始打印下一行
					if row_count > width:
						break

			print('\n', end='')

		print('\n', end='')


show("狗, 不是人类最好的朋友")
